package com.cn.aam.checaiduo.center;

import android.content.SharedPreferences;

import com.cn.aam.checaiduo.AppData;
import com.cn.aam.checaiduo.BuildConfig;
import com.cn.aam.checaiduo.bean.shareference.AppSharePre;
import com.google.gson.Gson;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by 艹羊 on 2017/2/23.
 */

public class SharePreferenceCenter {

    //单例模式
    private volatile static SharePreferenceCenter INSTANCE;//声明 volatiel 禁止指令重排序优化
    private static final String APP_SHAREPRE = "app_sharepre";


    private SharePreferenceCenter() {
    }

    public static SharePreferenceCenter getInstance() {
        return SharePreferenceCenter.Holder.INSTANCE;
    }

    private static class Holder {
         static SharePreferenceCenter INSTANCE = new SharePreferenceCenter();

    }

    /**
     * APP SharePrefrences
     */
    public AppSharePre getAppSharePre() {
        SharedPreferences sp = AppData.getContext().getSharedPreferences(BuildConfig.APPNAME, MODE_PRIVATE);
        String jsonConfig = sp.getString(APP_SHAREPRE, "");
        if (jsonConfig.equals("")) {
            AppSharePre appSharePre = new AppSharePre();
            setAppSharepre(appSharePre);
            return appSharePre;
        }
        return new Gson().fromJson(jsonConfig, AppSharePre.class);
    }

    public void setAppSharepre(AppSharePre appSharePre) {
        SharedPreferences sp = AppData.getContext().getSharedPreferences(BuildConfig.APPNAME, MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(APP_SHAREPRE, new Gson().toJson(appSharePre));
        editor.apply();
    }

    public void cleanAppSharepre() {
        SharedPreferences sp = AppData.getContext().getSharedPreferences(BuildConfig.APPNAME, MODE_PRIVATE);
        sp.edit().remove(APP_SHAREPRE).apply();
    }


    /**
     * 存储-账号-密码
     */
    public void savePhonePwd(String username, String password) {
        AppSharePre appSharePre = getAppSharePre();
        if (appSharePre == null)
            return;
        appSharePre.setUsername(username);
        appSharePre.setPassword(password);
        setAppSharepre(appSharePre);
    }


    /**
     * 存储
     * -账号
     */
    public void savePhone(String username) {
        AppSharePre appSharePre = getAppSharePre();
        if (appSharePre == null)
            return;
        appSharePre.setUsername(username);
        appSharePre.setPassword("");
        setAppSharepre(appSharePre);
    }

    //我的界面，资金是否可见
    public boolean isFinanceVisibility() {
        AppSharePre appSharePre = getAppSharePre();
        if (appSharePre == null)
            return true;
        return appSharePre.isFinanceVisibility();
    }

    public void setFinanceVisibility(boolean visibility) {
        AppSharePre appSharePre = getAppSharePre();
        if (appSharePre == null)
            return;
        appSharePre.setFinanceVisibility(visibility);
        setAppSharepre(appSharePre);
    }

    public void clearFinanceData() {
        AppSharePre appSharePre = getAppSharePre();
        if (appSharePre == null)
            return;
        appSharePre.setFinanceVisibility(true);
        setAppSharepre(appSharePre);
    }

}
